package com.matrix.user.controller;

import com.matrix.api.user.dto.UpdateNicknameDTO;
import com.matrix.api.user.vo.UserInfoVO;
import com.matrix.common.domain.Result;
import com.matrix.common.exception.BusinessException;
import com.matrix.user.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 有点甜
 * @since 2025/7/25
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/info")
    public Result<UserInfoVO> getCurrentUserInfo(@RequestHeader("X-User-ID") Long userId) {
        UserInfoVO userInfo = userService.getUserInfoByUid(userId);
        return Result.success(userInfo);
    }

    @PutMapping("/profile")
    public Result<UserInfoVO> updateUserProfile(@RequestHeader("X-User-ID") Long userId,
                                                @Valid @RequestBody UpdateNicknameDTO profileDTO) {
        return userService.updateUserProfile(userId, profileDTO);
    }

    @PutMapping("/avatar")
    public Result<Void> updateUserAvatar(@RequestHeader("X-User-ID") Long userId,
                                         @RequestBody Map<String, String> payload) {
        String avatarUrl = payload.get("avatarUrl");
        if (avatarUrl == null || avatarUrl.isBlank()) {
            throw new BusinessException("头像URL不能为空");
        }
        userService.updateUserAvatar(userId, avatarUrl);
        return Result.success();
    }

    @PostMapping("/favorites/{shopId}")
    public Result<Void> addFavorite(@RequestHeader("X-User-ID") Long userId,
                                    @PathVariable("shopId") Long shopId) {
        userService.addFavorite(userId, shopId);
        return Result.success();
    }

    @DeleteMapping("/favorites/{shopId}")
    public Result<Void> removeFavorite(@RequestHeader("X-User-ID") Long userId,
                                       @PathVariable("shopId") Long shopId) {
        userService.removeFavorite(userId, shopId);
        return Result.success();
    }

    @GetMapping("/favorites")
    public Result<List<Long>> listFavorites(@RequestHeader("X-User-ID") Long userId) {
        return Result.success(userService.listFavorites(userId));
    }

    @GetMapping("/favorites/{shopId}/status")
    public Result<Boolean> checkFavoriteStatus(@RequestHeader("X-User-ID") Long userId,
                                               @PathVariable("shopId") Long shopId) {
        return Result.success(userService.checkFavoriteStatus(userId, shopId));
    }

    @GetMapping("/search-history")
    public Result<List<String>> getSearchHistory(@RequestHeader("X-User-ID") Long userId) {
        return Result.success(userService.getSearchHistory(userId));
    }

    @PostMapping("/search-history")
    public Result<Void> addSearchHistory(@RequestHeader("X-User-ID") Long userId, @RequestBody Map<String, String> payload) {
        userService.addSearchHistory(userId, payload.get("keyword"));
        return Result.success();
    }

    @DeleteMapping("/search-history")
    public Result<Void> clearSearchHistory(@RequestHeader("X-User-ID") Long userId) {
        userService.clearSearchHistory(userId);
        return Result.success();
    }
}
